package com.nothing.emp.controller;

import com.nothing.common.util.Page;
import com.nothing.common.util.Result;
import com.nothing.emp.service.DeptService;
import com.nothing.emp.service.EmpService;
import com.nothing.emp.vo.DeptVo;
import com.nothing.emp.vo.EmpVo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/*
* 控制器
*
*
*
* */
@Controller
@RequestMapping("/emp")
public class EmpController {

    @Resource
    private EmpService empService;
    @Resource
    private DeptService deptService;
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        //注册自定义的属性编辑器
        //1、日期
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        //表示如果命令对象有 Date 类型的属性，将使用该属性编辑器进行类型转换
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    //跳转到列表页
    @RequestMapping(value = "/tolist",method = RequestMethod.GET)
    public String toList(){
        return "emp/list2";
    }

    //查询员工列表  所有列表
    @ResponseBody
    @RequestMapping(value = "/list")
    public Result list(){
        //查询所有员工的列表
        List empList = empService.queryAll();
        //组装json数据，响应Result对象
        Result r = Result.ok("查询列表数据成功");
        r.put("data",empList);
        return r;
    }

    /*//查询员工列表  分页
    @ResponseBody
    @RequestMapping(value = "/list2")
    public Result list2(Page page){
        //查询当前分页条件下的列表
        List empList = empService.queryByPage(page);

        //查询当前的总记录数
        int count = empService.queryCount();
        //组装json数据，响应Result对象
        Result r = Result.ok("查询列表数据成功");
        r.put("data",empList);
        r.put("count",count);
        return r;
    }*/

    /*//查询员工列表  分页  + 带条件筛选
    @ResponseBody
    @RequestMapping(value = "/list2")
    public Result list2(Page page , @RequestParam Map paramMap){
        //查询当前分页+条件下的列表
        List empList = empService.queryByPageAndParam(page,paramMap);

        //查询当前的总记录数
        int count = empService.queryCount();
        //组装json数据，响应Result对象
        Result r = Result.ok("查询列表数据成功");
        r.put("data",empList);
        r.put("count",count);
        return r;
    }*/


    //多表关联数据 + 分页 + 条件筛选
    @ResponseBody
    @RequestMapping(value = "/list2")
    public Result list2(Page page , @RequestParam Map paramMap){
        //查询当前分页+条件下的列表
        List empList = empService.queryBySql(page,paramMap);

        //查询当前的总记录数
        int count = empService.queryCount();
        //组装json数据，响应Result对象
        Result r = Result.ok("查询列表数据成功");
        r.put("data",empList);
        r.put("count",count);
        return r;
    }



    //跳转到新增页
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toAdd(){
        return "emp/add";
    }

    //新增
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(EmpVo empVo,HttpServletRequest request) throws IOException {
        boolean result = empService.save(empVo);
        Result r ;
        if(result){
            r = Result.ok("保存成功！");
        }else{
            r = Result.error("服务器异常！");
        }
        return r;
    }

    //图片上传
    @ResponseBody
    @RequestMapping(value = "/upload")
    public Result upload(@RequestParam(value = "file") MultipartFile faceImg,HttpServletRequest request) throws IOException {
        //获取原文件名  注意：原文件名要保存到数据库中
        String oldName = faceImg.getOriginalFilename();
        System.out.println("oldName:" + oldName);
        //获取原文件的后缀名
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        //防止文件名重复，创建一个随机的新文件名
        //1.当前毫秒数，非常精准
        //String newName = "" + System.currentTimeMillis() + "." + suffix;
        //2.使用java中的唯一标识符
        String newName = UUID.randomUUID().toString() + "." + suffix;
        //确定文件的保存路径
        String dirPath = request.getServletContext().getRealPath("/") + "upload";
        System.out.println("savePath:" + dirPath);
        //判断该文件夹是否存在
        File dirFile = new File(dirPath);
        if(!dirFile.isDirectory()){
            dirFile.mkdir();//如果该路径不存在一个文件夹，则给它创建一个文件夹
        }
        //文件在服务器的真实绝对路径
        String path = dirPath + "/" + newName;
        File file = new File(path);
        //将多媒体文件数据，写入到硬盘文件对象中
        faceImg.transferTo(file);
        //新文件的web路径
        String webPath = "/upload/" + newName;


        Result r  = Result.ok("上传成功");
        r.put("url",webPath);
        return r;
    }



    //删除
    @ResponseBody
    @RequestMapping("/delete/{empno}")
    public Result delete(@PathVariable(value = "empno") Integer empno){
        EmpVo empVo = new EmpVo();
        empVo.setEmpNo(empno);
        boolean result = empService.delete(empVo);
        //根据删除成功与否，做不同的响应
        Result r ;
        if(result){
            r = Result.ok("删除成功！");
        }else{
            r = Result.error(500,"服务器异常！");
        }
        return r;
    }

    //修改页
    @RequestMapping(value = "/edit/{empno}", method = RequestMethod.GET)
    public String toUpdate(@PathVariable(value = "empno") Integer empno ,HttpServletRequest request){
        request.setAttribute("empNo",empno);
        return "emp/edit";
    }

    //修改
    @RequestMapping(value = "/update" ,method = RequestMethod.POST)
    public String update(EmpVo empVo){
        //真正的修改操作，参考新增
        return "redirect:list";
    }

    //查看详情
    @RequestMapping(value = "/info/{empno}" )
    public String update(@PathVariable(value = "empno") Integer empno ,HttpServletRequest request){

        return "emp/empInfo";
    }

    //查询员工列表和部门列表  以及当前员工的信息
    @ResponseBody
    @RequestMapping("/queryEmpsAndDepts/{id}")
    public Result querySm(@PathVariable(value = "id") Integer id){

        //根据ID，查询员工信息
        EmpVo emp = empService.queryById(id);

        List<EmpVo> emps = empService.queryAll();
        List<DeptVo> depts = deptService.queryAll();

        Result r = Result.ok();
        r.put("empList",emps);
        r.put("deptList",depts);
        r.put("emp",emp);
        return r;
    }





}
